package org.study.itcast;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.study.ByteBufferUtil.debugAll;

/**
 * @author: jzhou
 * @date: 2024/4/19-22:10
 * @version: 1.0
 * @description: spark3.3-study
 */
public class TestByteBuffer {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);// class java.nio.HeapByteBuffer
        // ByteBuffer byteBuffer = ByteBuffer.allocateDirect(16);// class java.nio.DirectByteBuffer

        buffer.put(new byte[]{'a', 'b', 'd', 'f'});
        buffer.flip();

        buffer.get(new byte[4]);
        buffer.rewind();
        buffer.get(new byte[4]);
        debugAll(buffer);

        buffer.clear();
        // 字符串转bytebuffer
        buffer.put("hello".getBytes());
        debugAll(buffer);

        // charset 转bytebuffer
        ByteBuffer helloMoto = StandardCharsets.UTF_8.encode("hello moto");
        debugAll(helloMoto);

        // wrap
        ByteBuffer wrap = ByteBuffer.wrap("fire".getBytes());
    }
}
